package leetcodev1.字符串;

import leetcodev1.链表.Solution;

import java.util.HashMap;
import java.util.Map;

public class LeetCode13 {
    Map<Character, Integer> dict = new HashMap<Character, Integer>() {{
        put('I', 1);
        put('V', 5);
        put('X', 10);
        put('L', 50);
        put('C', 100);
        put('D', 500);
        put('M', 1000);
    }};

    //模拟-暴力
    //模拟运行过程是一种直观的思考过程
    public int romanToInt(String s) {
        int ret = 0, n = s.length();
        for (int i = 0; i < n - 1; i++) {
            char c = s.charAt(i);
            char next = s.charAt(i + 1);
            if (dict.get(c) < dict.get(next)) {
                ret -= dict.get(c);
            } else {
                ret += dict.get(c);
            }
        }
        ret += dict.get(s.charAt(n - 1));
        return ret;
    }
}
